package cn.dev.mybatis.handler.result;

import cn.dev.mybatis.util.ObjectUtils;

import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class BeanResultHandler<T> extends AbstractResultHandler<T> {
    private final Class<T> clazz;

    @SuppressWarnings("unchecked")
    public BeanResultHandler(Type type) {
        this.clazz = (Class<T>) type;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T handle(ResultSet rs, int colum) throws SQLException {
        Map<String, Object> objectMap = parseForMap(rs, colum);
        Object instance = ObjectUtils.newInstance(clazz);
        ObjectUtils.fillProperty(instance, objectMap);
        return (T) instance;
    }
}
